/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.demo.cards;

import it.gmariotti.cardslib.demo.ResourceTable;
import it.gmariotti.cardslib.library.Constants;
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardHeader;
import it.gmariotti.cardslib.library.internal.CardThumbnail;
import it.gmariotti.cardslib.library.utils.PixelMapUtils;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Rating;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.global.configuration.DeviceCapability;
import ohos.hiviewdfx.HiLog;

/**
 * This class provides a simple example as Google Play card
 *
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class GplayCard extends Card {

    public GplayCard(Context context) {
        super(context, ResourceTable.Layout_carddemo_gplay_inner_content);
        init();
    }

    public GplayCard(Context context, int innerLayout) {
        super(context, innerLayout);
        init();
    }

    private void init() {
        CardHeader header = new CardHeader(getContext());
        header.setButtonOverflowVisible(true);
        header.setTitle("Google Maps");
        //todo
        header.setPopupMenu(ResourceTable.Layout_menu_item_layout, (CardHeader.OnClickCardHeaderPopupMenuListener) (card1, item) -> new ToastDialog(getContext()).setText(item.getTitle()).show());
        addCardHeader(header);

        GoogleNowBirthThumb thumbnail = new GoogleNowBirthThumb(getContext());
        thumbnail.setDrawableResource(ResourceTable.Media_carddemo_ic_gmaps_large);
        addCardThumbnail(thumbnail);
    }

    @Override
    public void setupInnerViewElements(ComponentContainer parent, Component view) {

        Text title = (Text) view.findComponentById(ResourceTable.Id_carddemo_gplay_main_inner_title);
        title.setText("FREE");

        Text subtitle = (Text) view.findComponentById(ResourceTable.Id_carddemo_gplay_main_inner_subtitle);
        subtitle.setText("Very popular with...");

        Rating mRatingBar = (Rating) parent.findComponentById(ResourceTable.Id_carddemo_gplay_main_inner_ratingBar);

        try {
            mRatingBar.setFilledElement(PixelMapUtils.prepareElementById(getContext(), ResourceTable.Media_rate_star_small_on_holo_dark));
            mRatingBar.setUnfilledElement(PixelMapUtils.prepareElementById(getContext(), ResourceTable.Media_rate_star_small_off_holo_dark));
            mRatingBar.setHalfFilledElement(PixelMapUtils.prepareElementById(getContext(), ResourceTable.Media_rate_star_small_half_holo_dark));
            mRatingBar.setRatingItems(5);
            mRatingBar.setMaxValue(5);
            mRatingBar.setGrainSize(0.5f);
            mRatingBar.setScore(4.7f);
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }
    }

    class GoogleNowBirthThumb extends CardThumbnail {

        public GoogleNowBirthThumb(Context context) {
            super(context);
        }

        @Override
        public void setupInnerViewElements(ComponentContainer parent, Component viewImage) {


            //viewImage.getLayoutConfig().width = 196;
            //viewImage.getLayoutConfig().height = 196;
            if (viewImage != null) {
                if (parent != null && parent.getResourceManager() != null) {
                    DeviceCapability metrics = parent.getResourceManager().getDeviceCapability();

                    int base = 98;

                    if (metrics != null) {
                        viewImage.getLayoutConfig().width = (int) (base * metrics.screenDensity);
                        viewImage.getLayoutConfig().height = (int) (base * metrics.screenDensity);
                    } else {
                        viewImage.getLayoutConfig().width = 196;
                        viewImage.getLayoutConfig().height = 196;
                    }
                }
            }

        }
    }

    class GoogleNowBirthHeader extends CardHeader {

        String mName;
        String mSubName;

        public GoogleNowBirthHeader(Context context, int innerLayout) {
            super(context, innerLayout);
        }

        @Override
        public void setupInnerViewElements(ComponentContainer parent, Component view) {

            Text txName = (Text) view.findComponentById(ResourceTable.Id_text_birth1);
            Text txSubName = (Text) view.findComponentById(ResourceTable.Id_text_birth2);

            txName.setText(mName);
            txSubName.setText(mSubName);
        }
    }
}
